package com.project.nimsen.common.impl;

import java.io.File;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.FileCopyUtils;

import com.project.nimsen.common.FileUpload;
import com.project.nimsen.constant.NimsenConstant;
import com.project.nimsen.util.WebUtil;
import com.project.jdbc.utils.ClassUtils;

public class FileUploadWebInsert extends FileUpload {
	@Resource
	WebUtil webUtil;
	private File fin = null;
	private byte [] bin = null;
	private String srcFileName;
	private HttpServletRequest request;
	private String uploadPath;
	/**
	 * 上传一个文件，默认上传到网站根目录下upload文件夹下
	 * @param in 文件信息
	 * @param request
	 * */
	public FileUploadWebInsert(File in, HttpServletRequest request) {
		this.fin = in;
		this.request = request;
	
	}
	/**
	 * 上传一个文件，默认上传到网站根目录下upload文件夹下
	 * @param in 文件信息
	 * @param request
	 * @param 上传的文件夹
	 * */
	public FileUploadWebInsert(File in, HttpServletRequest request, String uploadPath) {
		this.fin = in;
		this.request = request;
		this.uploadPath = uploadPath;
	}
	/**
	 * 上传一个文件的二进制流，默认上传到网站根目录下upload文件夹下
	 * @param in 文件的二进制流
	 * @param srcFileName 源文件名
	 * @param request
	 * */
	public FileUploadWebInsert(byte[] in, String srcFileName, HttpServletRequest request){
		this.bin = in;
		this.srcFileName = srcFileName;
		this.request = request;
	}
	/**
	 * 上传一个文件的二进制流，默认上传到网站根目录下upload文件夹下
	 * @param in 文件的二进制流
	 * @param srcFileName 源文件名
	 * @param request
	 * @param 上传的文件夹
	 * */
	public FileUploadWebInsert(byte[] in, String srcFileName, HttpServletRequest request, String uploadPath){
		this.bin = in;
		this.srcFileName = srcFileName;
		this.request = request;
		this.uploadPath = uploadPath;
	}
	private String autoFilename;
	public String getAutoFilename() {
		return autoFilename;
	}
	private String rootPath;
	
	public String getRootPath() {
		return rootPath;
	}

	public String upload() throws Exception {
		//上传文件
		if(this.fin!=null){
			FileCopyUtils.copy(this.fin, initParam());
			fin.delete();
		}else
			FileCopyUtils.copy(this.bin, initParam());
		return srcFileName;
	}
	/**
	 * 初始化autoFilename与rootPath并生成要上传的文件
	 * @throws Exception 
	 * */
	private File initParam() throws Exception{
		String uuid = ClassUtils.autoUUID();
		//生成文件的文件名
		if(this.fin!=null){
			String filename = fin.getName();
			autoFilename = uuid + filename.substring(filename.lastIndexOf("."));
		}else
			autoFilename = uuid + this.srcFileName.substring(this.srcFileName.lastIndexOf("."));
		//文件跟径设置
		if(StringUtils.isBlank(uploadPath)){
			uploadPath = NimsenConstant.UPLOAD_PATH;
			//添加上管理员id
			uploadPath = uploadPath + File.separator + webUtil.currSite(request).getManagerid();
		}
		rootPath = WebUtil.webUrl(this.request)+File.separator+uploadPath;
		ServletContext sevcon = request.getSession().getServletContext();
		String allPath = sevcon.getRealPath(File.separator+uploadPath);
		File dir = new File(allPath);
		if (!dir.exists())
			dir.mkdirs();
		return new File(allPath+File.separator+autoFilename);
	}

}
